<template>
  <div class="view-container notice-list">
    <header class="view-header">
      <div class="header-container">
        <div class="header-title">
          <i class="el-icon-s-grid"></i>
          <span>已办事项</span>
        </div>
        <div class="header-form">
          <!--更多-->
          <div class="header-form-item">
            <el-dropdown @command="handleCommand">
              <el-button type="default" :loading="delLoading">
                更多<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-refresh-right" class="color-refresh-item" command="refresh">刷新</el-dropdown-item>
                <el-dropdown-item icon="el-icon-edit" class="color-edit-item" command="edit">编辑</el-dropdown-item>
                <el-dropdown-item icon="el-icon-delete" class="color-del-item" command="del">{{isBatchesDel}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <!--搜索-->
          <div class="header-form-item">
            <el-button plain type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
          </div>

        </div>
      </div>
    </header>
    <section class="table-container view-section">
      <el-table
        ref="table"
        :data="tableData"
        stripe
        border
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column type="selection" align="center"/>
        <el-table-column sortable prop="letterTitle" label="标题" align="center"/>
        <el-table-column sortable prop="letterType" label="督办类别" align="center"/>
        <el-table-column sortable prop="letterSource" label="办理事项" align="center"/>
        <el-table-column sortable prop="fillDate" label="拟交办日期" align="center"/>
        <el-table-column sortable prop="latestDate" label="办结期限" align="center"/>
      </el-table>
    </section>
    <footer>
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        style="text-align: right"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        :page-size="page.size"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </footer>

  </div>
</template>

<script>
  import letterAPI from '@/api/letter/letter'
  export default {
    name: 'LetterDaiBan',
    data() {
      return {
        tableData: [],
        letter:{
          letterId:'',
          letterTitle:'',
          letterType:'',
          letterSource:'',
          fillDate:'',
          peiHeRen:'',
          contactTel:'',
          niBanYiJian:'',
          latestDate:'',
          letterContent:'',
          zeRenRen:0,
          letterState:0,
          unitId:0,
          creator:0,
          signIndex:0,
          signCount:0
        },
        dateArr: [],
        page: {
          index: 1,
          size: 10,
          total: 0
        },
        multipleSelected: [],
        delLoading: false,
      }
    },
    created() {
      this.fetchData()
    },
    computed:{
      isBatchesDel(){
        return this.multipleSelected.length > 1 ? '批量删除' : '删除'
      }
    },
    methods: {
      handleRowClick(row,column,event){
        this.$refs['table'].toggleRowSelection(row)
      },
      refresh(){
        location.reload()
      },
      fetchData() {
        letterAPI.getMyDoneList(this.page).then(res => {
          this.page.total = res.total
          this.tableData = res.records
        })
      },
      handleCommand(command) {
        switch (command) {
          case 'edit':
            this.edit()
            break;
          case 'del':
            this.del()
            break;
          case 'refresh':
            this.refresh()
            break;
          default:
        }
      },
      edit() {
        if (this.multipleSelected.length === 0) {
          this.$message.warning('请选择要编辑的行')
        } else {
          // let-todo 编辑的地址
          this.$router.push('/portal/edit-notice/' + this.multipleSelected[0])
        }
      },
      del() {
        if (this.multipleSelected.length === 0) {
          this.$message.warning('请选择要删除的行')
          return
        }
        if (this.delLoading) {
          return
        }
        this.$confirm('确定删除吗', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delLoading = true
          // const promise = this.multipleSelected.length > 1 ? receiptAPI.deleteBatch(this.multipleSelected) : receiptAPI.delete(this.multipleSelected[0])
          const promise = receiptAPI.deleteBatch(this.multipleSelected)
          promise.then(res => {
            this.delLoading = false
            if (res.success) {
              this.$message.success('删除成功')
              if(this.multipleSelected.length === this.tableData.length){
                this.page.index = --this.page.index === 0 ? 1 : this.page.index
              }
              this.fetchData()
            } else {
              this.$message.warning('删除失败')
            }
          }).catch(err => {
            this.delLoading = false
            console.log(err)
          })
        }).catch(() => {
        })
      },
      handleSelectionChange(arr) {
        this.multipleSelected = []
        for (const val of arr) {
          this.multipleSelected.push(val.docId)
        }
      },
      toAddPage() {
        this.$router.push('/portal/add-notice')
      },
      handleSizeChange(val) {
        this.page.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.page.index = val
        this.fetchData()
      }
    }
  }
</script>

<style lang="scss">

</style>

<style scoped lang="scss">

</style>


